<template>
  <vxe-grid
    ref="xTable"
    border
    resizable
    keep-source
    align="center"
    height="300px"
    :edit-rules="validRules"
    :edit-config="{trigger: 'click', mode: 'row', showStatus: true}"
    :columns="tableColumn"
    :data="dataSource.length>0?dataSource:levels"
  >
  </vxe-grid>
</template>
<script >
import { filterDictTextByCache, initDictOptions } from '@/components/dict/JDictSelectUtil'
export default {
  name: 'ScoreTable',
  props: {
    dataSource: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      loading: false,
      tableColumn: [
        { type: 'seq', width: 60 },
        { field: 'levelDesc', title: '级别', slots: { default: ({ row }) => {
          return filterDictTextByCache('hr_attendance_level', row.levelDesc)
        } }},
        { field: 'scoreLower', title: '得分/排名下限', editRender: { name: '$input', props: { type: 'number' }}},
        { field: 'scoreSuper', title: '得分/排名上限', editRender: { name: '$input', props: { type: 'number' }}},
        { field: 'assessmentScore', title: '绩效比例', editRender: { name: '$input', props: { type: 'number' }}}
      ],
      validRules: {
        scoreLower: [
          { required: true, message: '请填写得分/排名下限' },
          { min: 0, type: 'number', message: '请填写大于等于0的数字' }
          // { validator: this.score }
        ],
        scoreSuper: [
          { required: true, message: '请填写得分/排名上限' },
          { min: 0, type: 'number', message: '请填写大于等于0的数字' }
          // { validator: this.score }
        ],
        assessmentScore: [
          { required: true, message: '请填写绩效比例' },
          { min: 0, type: 'number', message: '请填写大于等于0的数字' }
        ]
      },
      levels: []
    }
  },
  methods: {
    score({ cellValue, row }) {
      const { fullData } = this.$refs.xTable.getTableData()
      let total = 0
      fullData.map((item, i) => {
        if (row._XID !== item._XID) {
          total += Number(item.score)
        } else {
          total += Number(cellValue)
        }
      })
      if (total !== 100) {
        return new Error('排名之和必须等于100')
      }
    },
    validate() {
      return this.$refs.xTable.validate(true)
    },
    getTableData() {
      return this.$refs.xTable.getTableData()
    },
    initDictConfig() {
      initDictOptions('hr_attendance_level').then(res => {
        if (res.success) {
          this.levels = []
          res.result.forEach(item => {
            this.levels.push({
              levelDesc: item.value
            })
          })
        }
      })
    }
  },
  mounted() {
    this.initDictConfig()
  }
}
</script>
